package cn.sinjet.prototol.codec;

import java.util.Calendar;

import android.util.Log;
import cn.sinjet.communication.bluetooth.BluetoothModel;

public class ToDevNaviCodec {
	/*private boolean isNaviDev = false;
	public void setIsHudDev(boolean isHud){
		isNaviDev = isHud;
	}
	public boolean isNaviDev(){
		return isNaviDev;
	}*/
	private void packAndSendMsg(byte[] message,int len){
	    byte[] szMsg = new byte[len + 5];
		szMsg[0] = (byte) 0xFF;
		szMsg[1] = (byte) 0x55;
		szMsg[2] = (byte) (len + 1);
		szMsg[3] = (byte) 0x01;
		System.arraycopy(message, 0, szMsg, 4, len);

		byte sum = 0;
		int s = 0;
		int i = 0;
		for (i = 2; i < len + 4; i++) {
			s += (szMsg[i] & 0xFF);
		}
		sum = (byte) (s & 0xFF);
		szMsg[len + 4] = sum;
		BluetoothModel.getInstance().sendMessage(szMsg);
    }
	
//	  public void initMCU(){
//	 	   byte[] buf = new byte[]{0x0,0x1};
//	 	   packAndMsg(buf,2);
//	    }
	
	    public void sendnaviInfo(int iconType,int distance){
//	    	if(!isNaviDev) return;
	 	     byte[] buf = new byte[6];
	 	     buf[0] = 0x0;
	 	     buf[1] = (byte)iconType;
	 	     buf[2] = (byte)(distance&0xFF);
	 	     buf[3] = (byte)((distance&0xFF00)>>8);
	 	     buf[4] = (byte)((distance&0xFF0000)>>16);
	 	     buf[5] = (byte)((distance&0xFF000000)>>24);
	 	     packAndSendMsg(buf, 6);
	    }
	    
	    
		public void sendLineInfo(int retainDistance) {
			// TODO Auto-generated method stub
//			if(!isNaviDev) return;
			byte[] buf = new byte[5];
	 	     buf[0] = 0x2;
	 	     buf[1] = (byte)(retainDistance&0xFF);
	 	     buf[2] = (byte)((retainDistance&0xFF00)>>8);
	 	     buf[3] = (byte)((retainDistance&0xFF0000)>>16);
	 	     buf[4] = (byte)((retainDistance&0xFF000000)>>24);
	 	     packAndSendMsg(buf, 5);
		}
		
		public void notifyIsNaviRunning(byte flag){
//			if(!isNaviDev) return;
			Log.d("test", "notifyIsNaviRunning:"+flag);
			byte[] buf = new byte[]{(byte)0xE0,flag};
			packAndSendMsg(buf, 2);
		}
		public void sendLimitSpeed(int speed, int limitSpeed) {
//			if(!isNaviDev) return;
			// TODO Auto-generated method stub
			byte[] buf = new byte[5];
	 	     buf[0] = 0x1;
	 	     buf[1] = (byte)(speed&0xFF);
	 	     buf[2] = (byte)((speed&0xFF00)>>8);
	 	     buf[3] = (byte)(limitSpeed&0xFF);
	 	     buf[4] = (byte)((limitSpeed&0xFF00)>>8);
	 	     packAndSendMsg(buf, 5);
		}
		public void sendCameraDistance(int distance,int dsp,int type) {
			// TODO Auto-generated method stub
//			if(!isNaviDev) return;
			byte[] buf = new byte[5];
	 	     buf[0] = 0x10;
	 	     buf[1] = (byte)(distance&0xFF);
	 	     buf[2] = (byte)((distance&0xFF00)>>8);
	 	     buf[3] = (byte)(dsp);
	 	     buf[4] = (byte)(type);
	 	     packAndSendMsg(buf, 5);
		}
		
		public void sendLaneInfo(int[] laneInfo) {
			// TODO Auto-generated method stub
//			if(!isNaviDev) return;
			if(laneInfo == null){
			    byte[] buf = new byte[2];
			    buf[0] = 0x11;
			    buf[1] = 0x0;
				packAndSendMsg(buf, buf.length);				
			}else{
				byte[] buf = new byte[2+laneInfo.length];
				buf[0] = 0x11;
				buf[1] = (byte)laneInfo.length;
				for(int i=2;i<buf.length;i++)
					buf[i] = (byte)laneInfo[i-2];
			    packAndSendMsg(buf, buf.length);
		 }
	 }
		public void sendCurrentTime() {
			// TODO Auto-generated method stub
//			if(!isNaviDev) return;
			long time = System.currentTimeMillis();
		    final Calendar calendar = Calendar.getInstance();
		    calendar.setTimeInMillis(time);
		    int hour = calendar.get(Calendar.HOUR_OF_DAY);
		    int minute = calendar.get(Calendar.MINUTE);
		    int second = calendar.get(Calendar.SECOND);
			byte buf[] = new byte[4];
			buf[0] = 0x07;
			buf[1] = (byte)hour; 
			buf[2] = (byte)minute;
			buf[3] = (byte)second;
			Log.d("ing", "sendCurrentTime"+hour+","+minute+","+second);
			packAndSendMsg(buf, buf.length);
		}
}
